<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="50%">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="租户名称" prop="tenantName">
          <el-input
            class="!w-240px"
            v-model="queryParams.tenantName"
            placeholder="请输入租户名称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="岗位名称" prop="name">
          <el-input
            class="!w-240px"
            v-model="queryParams.name"
            placeholder="请输入岗位名称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>
    <ContentWrap>
      <el-table v-loading="loading" :data="list">
        <el-table-column label="租户ID" align="center" prop="tenantId" />
        <el-table-column label="租户名称" align="center" prop="tenantName" />

        <el-table-column label="岗位名称" align="center" prop="name" />
        <el-table-column label="岗位编码" align="center" prop="code" />
        <el-table-column label="岗位来源" align="center" prop="sourceType">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.TENANT_IDENTITY" :value="scope.row.sourceType" />
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="是否修改" align="center" prop="status">
          <template #default="scope">
            <el-tag type="danger">
              {{ scope.row.isUpdate ? '是' : '否' }}
            </el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          width="180"
          :formatter="dateFormatter"
        /> -->
        <el-table-column label="操作" align="center" min-width="120">
          <template #default="scope">
            <el-button
              v-hasPermi="['system:permission:assign-post-menu']"
              link
              preIcon="ep:open"
              title="重启"
              :disabled="scope.row.status == 0"
              type="primary"
              @click="resetPost(scope.row)"
            >
              重启
            </el-button>
            <el-button
              v-hasPermi="['system:permission:assign-post-menu']"
              link
              preIcon="ep:basketball"
              title="菜单权限"
              type="primary"
              @click="openAssignMenuFormOther(scope.row)"
            >
              菜单权限
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
  </Dialog>
  <PostCascadeAssignMenuForm ref="postCascadeAssignMenuFormRef" @success="getList" />
</template>
<script lang="ts" setup>
// todo 目前不用
import { DICT_TYPE } from '@/utils/dict'
import * as PostApi from '@/api/system/post'
import PostCascadeAssignMenuForm from './PostCascadeAssignMenuForm.vue'
import { ElMessageBox } from 'element-plus'
const { t } = useI18n()

defineOptions({ name: 'PostCascadeList' })

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const postType = ref() //岗位类型
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  parentId: 0,
  name: '',
  tenantName: ''
})
const queryFormRef = ref() // 搜索的表单

/** 查询级联跨租户岗位 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PostApi.getPostCascadePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 打开弹窗 */
const open = async (row?: any) => {
  dialogVisible.value = true
  dialogTitle.value = row.name + '岗位级联租户'
  // 修改时，设置数据
  if (row.id) {
    loading.value = true
    try {
      queryParams.parentId = row.id
      postType.value = row.type
      getList()
    } finally {
      loading.value = false
    }
  }
} /** 平台类型 和所选的类型 不一样时 菜单权限操作 */
const postCascadeAssignMenuFormRef = ref()
const openAssignMenuFormOther = async (row: PostApi.PostVO) => {
  row.type = postType.value
  postCascadeAssignMenuFormRef.value.open(row)
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
/**重启岗位 */
const resetPost = (row: PostApi.PostVO) => {
  ElMessageBox.confirm('确认重启该岗位吗？', '提示', {
    confirmButtonText: t('common.ok'),
    cancelButtonText: t('common.cancel')
  })
    .then(async () => {
      let params = {
        tenantId: row.tenantId,
        resetPostId: row.id
      }
      await PostApi.resetPost(params)
      await getList()
    })
    .catch(() => console.info('操作取消'))
}
</script>
